package com.egao.orderB.biz.impl;

import com.egao.common.core.damon.constant.UserConstant;
import com.egao.common.core.damon.results.BaseDao;
import com.egao.common.core.damon.results.PageResult2;

import com.egao.common.core.damon.utils.Utils;
import com.egao.common.system.entity.User;
import com.egao.orderB.biz.StockBiz;
import com.egao.orderB.entity.PutGoods;
import com.egao.orderB.entity.Stock;
import com.egao.common.core.web.JsonResult;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2021-12-23 16:30:08
 * @version ：1.0
 */
@Repository
public class StockBizImpl extends BaseDao implements StockBiz {

	@Override
	public JsonResult list(PageResult2<T> page, Stock stock, User loginUser) throws Exception {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<Stock> rm = new BeanPropertyRowMapper<Stock>(Stock.class);
		StringBuffer sql = new StringBuffer("select s.id,s.customer_id,s.customer");
    sql.append(",s.sku,s.name,s.num");
		sql.append(",s.pack,s.weight,s.length,s.width");
		sql.append(",s.high,s.pallet_num");
//		sql.append("s.volume,s.total_volume");
		sql.append(",s.order_num,s.create_time,s.create_user,s.update_time");
		sql.append(",s.update_user");
    sql.append(",suser.nick_name createName ");
    sql.append(" from chf_stock s ");
    sql.append(" inner join sys_user suser on suser.user_id=s.create_user ");
    sql.append(" where s.deleted=0 ");
    if(Utils.isNotEmpty(stock)){
			if(Utils.isNotEmpty(stock.getCustomer())){
				sql.append(" and instr(s.customer,:customer)>0 ");
				map.put("customer", stock.getCustomer());
			}
      if(Utils.isNotEmpty(stock.getSort())&&Utils.isNotEmpty(stock.getOrder())){
        sql.append(" order by s."+stock.getSort()+" "+stock.getOrder());
      } else {
        sql.append(" order by s.create_time desc");
      }
		}
		return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
	}

	@Override
	public List<Stock> list(Stock stock, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<Stock> rm = new BeanPropertyRowMapper<Stock>(Stock.class);
    StringBuffer sql = new StringBuffer("select s.id,s.customer_id,s.customer");
    sql.append(",s.sku,s.name,s.num");
		sql.append(",s.pack,s.weight,s.length,s.width");
		sql.append(",s.high,s.pallet_num");
//		sql.append("s.volume,s.total_volume");
		sql.append(",s.order_num,s.create_time,s.create_user,s.update_time");
		sql.append(",s.update_user");
    sql.append(",suser.nick_name createName ");
    sql.append(" from chf_stock s ");
    sql.append(" inner join sys_user suser on suser.user_id=s.create_user ");
    sql.append(" where s.deleted=0 ");
    if(Utils.isNotEmpty(stock)){
			if(Utils.isNotEmpty(stock.getCustomer())){
				sql.append(" and instr(s.customer,:customer)>0 ");
				map.put("customer", stock.getCustomer());
			}
      if(Utils.isNotEmpty(stock.getSort())&&Utils.isNotEmpty(stock.getOrder())){
        sql.append(" order by s."+stock.getSort()+" "+stock.getOrder());
      } else {
        sql.append(" order by s.create_time desc");
      }
    }
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public List<Stock> listUnique(Stock stock) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<Stock> rm = new BeanPropertyRowMapper<Stock>(Stock.class);
    StringBuffer sql = new StringBuffer("select s.id,s.customer_id,s.customer");
    sql.append(",s.sku,s.name,s.num");
		sql.append(",s.pack,s.weight,s.length,s.width");
		sql.append(",s.high,s.pallet_num");
//		sql.append("s.volume,s.total_volume");
		sql.append(",s.order_num,s.create_time,s.create_user,s.update_time");
		sql.append(",s.update_user");
    sql.append(",suser.nick_name createName ");
    sql.append(" from chf_stock s ");
    sql.append(" inner join sys_user suser on suser.user_id=s.create_user ");
    sql.append(" where s.deleted=0 ");
    if(Utils.isNotEmpty(stock)){
				sql.append(" and s.customer_id=:customer_id ");
				map.put("customer_id", stock.getCustomerId()+"");

				sql.append(" and s.sku=:sku ");
				map.put("sku", stock.getSku());

				sql.append(" and s.name=:name ");
				map.put("name", stock.getName());

				sql.append(" and s.pack=:pack ");
				map.put("pack", stock.getPack());


				sql.append(" and s.length=:length ");
				map.put("length", stock.getLength()+"");

				sql.append(" and s.width=:width ");
				map.put("width", stock.getWidth()+"");

				sql.append(" and s.high=:high ");
				map.put("high", stock.getHigh()+"");
    }
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public Stock getById(Stock stock) {
		RowMapper<Stock> rm = new BeanPropertyRowMapper<Stock>(Stock.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select s.id,s.customer_id,s.customer");
    sql.append(",s.sku,s.name,s.num");
		sql.append(",s.pack,s.weight,s.length,s.width");
		sql.append(",s.high,s.pallet_num");
//		sql.append("s.volume,s.total_volume");
		sql.append(",s.order_num,s.create_time,s.create_user,s.update_time");
		sql.append(",s.update_user");
    sql.append(",suser.nick_name createName ");
    sql.append(" from chf_stock s ");
    sql.append(" inner join sys_user suser on suser.user_id=s.create_user ");
    sql.append(" where s.deleted=0 and s.id=:id ");
		map.put("id", stock.getId());
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}

	@Override
	public boolean save(Stock stock, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into chf_stock (");
    sql.append("customer_id,customer,sku,name");
		sql.append(",num,pack,weight");
		sql.append(",length,width,high");
//		sql.append("volume,total_volume");
		sql.append(",pallet_num,order_num,create_user,create_time) values ( ");
    sql.append(":customer_id,:customer,:sku,:name");
		sql.append(",:num,:pack,:weight");
		sql.append(",:length,:width,:high");
//		sql.append(",:volume,:total_volume");
		sql.append(",:pallet_num,:order_num,:create_user,now())");
    map.put("customer_id", stock.getCustomerId()==null?null:stock.getCustomerId()+"");
    map.put("customer", stock.getCustomer());
    map.put("sku", stock.getSku());
    map.put("name", stock.getName());
    map.put("num", stock.getNum()+"");
    map.put("pack", stock.getPack());
    map.put("weight", stock.getWeight()+"");
    map.put("length", stock.getLength()+"");
    map.put("width", stock.getWidth()+"");
    map.put("high", stock.getHigh()+"");
//    map.put("volume", stock.getVolume());
//    map.put("total_volume", stock.getTotalVolume());
    map.put("pallet_num", stock.getPalletNum());
    map.put("order_num", stock.getOrderNum()==null?null:stock.getOrderNum()+"");
		map.put("create_user", loginUser.getUserId() + "");
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean update(Stock stock, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update chf_stock set ");
    sql.append("customer_id=:customer_id,customer=:customer,sku=:sku ");
		sql.append(",name=:name,num=:num ");
		sql.append(",pack=:pack,weight=:weight,length=:length ");
//		sql.append(",volume=:volume ,total_volume=:total_volume ");
		sql.append(",width=:width,high=:high ");
		sql.append(",pallet_num=:pallet_num,order_num=:order_num ");
		sql.append(",update_time=now(),update_user=:update_user where id=:id ");
    map.put("customer_id", stock.getCustomerId()==null?null:stock.getCustomerId()+"");
    map.put("customer", stock.getCustomer());
    map.put("sku", stock.getSku());
    map.put("name", stock.getName());
    map.put("num", stock.getNum()+"");
    map.put("pack", stock.getPack());
    map.put("weight", stock.getWeight()+"");
    map.put("length", stock.getLength()+"");
    map.put("width", stock.getWidth()+"");
    map.put("high", stock.getHigh()+"");
//    map.put("volume", stock.getVolume());
//    map.put("total_volume", stock.getTotalVolume());
    map.put("pallet_num", stock.getPalletNum());
    map.put("order_num", stock.getOrderNum()==null?null:stock.getOrderNum()+"");
		map.put("update_user", loginUser.getUserId() + "");
		map.put("id", stock.getId());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean updateNum(Stock stock, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update chf_stock set ");
    sql.append(" num=num + :num ");
//    sql.append(" ,total_volume=total_volume + :total_volume ");
		sql.append(",update_time=now(),update_user=:update_user where id=:id ");
    map.put("num", stock.getNum()+"");
//    map.put("total_volume", stock.getTotalVolume());
    map.put("update_user", loginUser.getUserId()+"");
    map.put("id", stock.getId());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean remove(Stock stock, User loginUser) {
		int count = 0;
		Map<String, String> map = null;
		if(stock.getId().length()>0){
			for(String ID : stock.getId().split(",")){
				map  = new HashMap<String, String>();
				if(ID==null || ID.equals("")){ break; }
				StringBuffer sql =new StringBuffer("update chf_stock set deleted=1" +
						",update_user=:update_user, update_time=now() "
						+ " where id =:id ");
				map.put("update_user", loginUser.getUserId() + "");
				map.put("id", ID);
				getMapParameter(sql, map);
				count = jdbcTemplate.update(sql.toString(),map);
			}
		}
		return count > 0;
	}
}
